#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int colour[20001];

int head[20001];

int weight=0;

struct Edge{
	int begin;
	int end;
	int next;
	int weight;
};

Edge edges[200001];

Edge sortEdges[100001];
int size=1;
int n,m;

int partition(int first,int last){
	Edge pivor=sortEdges[first];
	int low=first+1;
	int high=last;
	
	while(high>low){
		while(low<=high&&sortEdges[low].weight<=pivor.weight){
			low++;
		}
		
		while(low<=high&&sortEdges[high].weight>pivor.weight){
			high--;
		}
		
		if(high>low){
			Edge temp=sortEdges[low];
			sortEdges[low]=sortEdges[high];
			sortEdges[high]=temp;
		}
	}
	if(pivor.weight>sortEdges[high].weight){
        sortEdges[first]=sortEdges[high];
        sortEdges[high]=pivor;
        return high;
    }else {
        return first;
    }
}

void insert(int a,int b,int c){
	edges[size].begin=a;
	edges[size].end=b;
	edges[size].weight=c;
	edges[size].next=head[a];
	head[a]=size;
	size++;
	
	edges[size].begin=b;
	edges[size].end=a;
	edges[size].weight=c;
	edges[size].next=head[b];
	head[b]=size;
	size++;
}

void sort(int first,int last){
	if(last>first){
		int p=partition(first,last);
		sort(first,p-1);
		sort(p+1,last);
	}
}

void sort(){
	sort(1,m);
}

void change(int point,int col,bool* hasChange){
	colour[point]=col;
	int next=head[point];
	int target=edges[next].end;
	while(next!=0){
		target=edges[next].end;
		if(hasChange[target]==false&&edges[next].weight>=weight&&colour[target]==colour[point]){
			hasChange[target]=true;
			change(target,3-col,hasChange);
		}
		next=edges[next].next;
	}
}

bool draw(Edge edg){
	if(colour[edg.begin]==0&&colour[edg.end]==0){
		colour[edg.begin]=1;
		colour[edg.end]=2;
	}else if(colour[edg.begin]!=0&&colour[edg.end]==0){
		colour[edg.end]=3-colour[edg.begin];
	}else if(colour[edg.begin]==0&&colour[edg.end]!=0){
		colour[edg.begin]=3-colour[edg.end];
	}else if(colour[edg.begin]==colour[edg.end]){
		bool hasChange[200001];
		memset(hasChange,0,sizeof(hasChange));
		int col=3-colour[edg.begin];
		change(edg.begin,3-colour[edg.begin],hasChange);
		if(col!=colour[edg.begin]){
			return false;
		}
	}
	return true;
}

int main(){
	scanf("%d%d",&n,&m);
	
	for(int i=0;i<m;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		insert(a,b,c);
	}
	
	for(int i=1;i<=m;i++){
		sortEdges[i]=edges[2*i-1];
	}
	
	sort();
	
	int i=m;
	while(draw(sortEdges[i])&&i>0){
		i--;
		weight=sortEdges[i].weight;
	}
	
	cout<<sortEdges[i].weight;
	
	return 0;
}
